package com.spyrx7.fang_erhuo.util;

import android.content.Context;
import android.content.SharedPreferences;


import com.spyrx7.fang_erhuo.base.BaseApplication;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by pc-spyrx7 on 2015/12/31.
 * 作者  pc-spyrx7
 * 邮箱 spyhanfeng@qq.com
 */
public class PreferencesHelper {

    private SharedPreferences pref;
    private Map<String, Object> prefCache;

    private static PreferencesHelper instance = null;

    public PreferencesHelper() {
        this.pref = BaseApplication.getInstance().getSharedPreferences("share_private", Context.MODE_PRIVATE);
        this.prefCache = new HashMap();
    }

    public static PreferencesHelper getInstance() {
        if (instance == null) {
            instance = new PreferencesHelper();
        }
        return instance;
    }

    public void remove(String paramString) {
        if (!UIHelp.DEBUG) paramString = Base64Tools.encode(paramString);
        this.prefCache.remove(paramString);
        this.pref.edit().remove(paramString).commit();
    }

    public void putInt(String key, int value) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        this.prefCache.put(key, value);
        this.pref.edit().putInt(key, value).commit();
    }

    public int getInt(String key) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if (this.prefCache.containsKey(key)) {
            return (int) prefCache.get(key);
        }
        return this.pref.getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if (this.prefCache.containsKey(key)) {
            return (int) prefCache.get(key);
        }
        return this.pref.getInt(key, defaultValue);
    }

    public void putLong(String key, long value) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        this.prefCache.put(key, value);
        this.pref.edit().putLong(key, value).commit();
    }

    public Long getLong(String key) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if (this.prefCache.containsKey(key)) {
            return (Long) prefCache.get(key);
        }
        return this.pref.getLong(key, 0);
    }

    public void putString(String key, String value) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        this.prefCache.put(key, value);
        this.pref.edit().putString(key, value).commit();
    }

    public String getString(String key) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if (this.prefCache.containsKey(key)) {
            return (String) prefCache.get(key);
        }
        return this.pref.getString(key, "");
    }

    public void putFloat(String key,Float value){
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        this.prefCache.put(key, value);
        this.pref.edit().putFloat(key, value).commit();
    }

    public  Float getFloat(String key){
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if(this.prefCache.containsKey(key)){
            return (Float) prefCache.get(key);
        }
        return this.pref.getFloat(key,0);
    }

    public void putBoolean(String key, Boolean value) {
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        this.prefCache.put(key, value);
        this.pref.edit().putBoolean(key, value).commit();
    }

    public  boolean getBoolean(String key){
        if (!UIHelp.DEBUG) key = Base64Tools.encode(key);
        if(this.prefCache.containsKey(key)){
            return (boolean) prefCache.get(key);
        }
        return this.pref.getBoolean(key,false);
    }
}
